define(['swiper', 'jq', 'common'], (Swiper) => {
    var mySwiper = new Swiper('.swiper', {
        autoplay: true,
        loop: true,
        fadeEffect: {
            crossFade: true
        },
        initialSlide: 1,
        // centeredSlides: true,
        effect: "fade",
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
            transition: 1,
        },
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },

    })
    $('.sale>div').on('click', function () {
        location.href = 'html/list.html'
    })
    $('.name').on('click',function(){
        location.href = '../html/self_info.html'
    })
    //尾部复用
    // $('footer').load('./html/footer.html')
    // 吸顶(楼层)
    $(window).scroll(function () {
        if ($(window).scrollTop() > 126) {
            $('.navBottom').css({
                'position': 'fixed',
                'top': '0'
            })
        } else if ($(window).scrollTop() <= 126) {
            $('.navBottom').css({
                'position': 'absolute',
                'top': '126px'
            })
        }
        if ($('html').scrollTop() >= $('#banner').offset().top) {
            $('.floor1').fadeIn(200);
            $('.floor2').fadeIn(200);
        } else {
            $('.floor1').fadeOut(200);
            $('.floor2').fadeOut(200);
        }
    })
    $('.floor1>li').click(function () {
        $('html').animate({
            scrollTop: $('main>div').eq($(this).index()).offset().top - 75
        }, 500)
    })
    $('.floor2>li:last').children().click(function () {
        $('html').animate({
            scrollTop: 0
        }, 500)
    })

    // 选项卡
    $('.menu>ul>li').on('mouseover', function (e) {
        // console.log($(this).index());
        $(this).addClass('active').siblings().removeClass('active')
        $('.content>div').eq($(this).index()).addClass('show').siblings().removeClass('show')
        $('.content').css('display', 'block')
        // console.log($('.content>div'));
    })
    $('.menu').on('mouseleave', function (e) {
        $('.content').css('display', 'none')
        $('.menu>ul>li').removeClass('active')
    })

    //抢购倒计时
    var endDate = new Date('2022-11-11 00:00:00')
    var spanH = document.querySelector('.h')
    var spanM = document.querySelector('.m')
    var spanS = document.querySelector('.s')
    var spanH1 = document.querySelectorAll('.h')[1]
    var spanM1 = document.querySelectorAll('.m')[1]
    var spanS1 = document.querySelectorAll('.s')[1]
    //获取现在的时间
    setInterval(function () {
        var nowDate = new Date()
        var secondes = parseInt((endDate.getTime() - nowDate.getTime()) / 1000)
        var day = parseInt(secondes / 3600 / 24)
        var hours = parseInt(secondes / 3600 % 24)
        var minutes = parseInt(secondes / 60 % 60)
        var s = parseInt(secondes % 60)
        day < 10 ? day = '0' + day : day
        hours < 10 ? hours = '0' + hours : hours
        minutes < 10 ? minutes = '0' + minutes : minutes
        s < 10 ? s = '0' + s : s
        spanH.innerHTML = hours
        spanM.innerHTML = minutes
        spanS.innerHTML = s
        spanH1.innerHTML = hours
        spanM1.innerHTML = minutes
        spanS1.innerHTML = s
    }, 1000)

    // 登录
    let name = document.querySelector('.name')
    let out = document.querySelector('.out')
    let login = document.querySelector('.login')
    let user = getCookie('username')
    let em = document.querySelector('em')
    if (user) {
        name.style.display = 'block'
        name.innerHTML = `欢迎：${user}`
        out.style.display = 'block'
        login.style.display = 'none'
        em.style.display = 'block'
    } else {
        name.style.display = 'none'
        out.style.display = 'none'
        login.style.display = 'block'
        em.style.display = 'none'
    }
    out.onclick = function () {
        name.style.display = 'none'
        out.style.display = 'none'
        login.style.display = 'block'
        em.style.display = 'none'
        delCookie('username')
    }

    // 下拉菜单
    class Xiala {
        constructor() {
            this.caidan = document.querySelectorAll('.caidan')
            this.nav = document.querySelector('.nav')
            this.catalog = document.querySelectorAll('.catalog')
            this.init()
        }
        init() {
            this.add()
        }
        add() {
            this.nav.addEventListener('mouseover', (e) => {
                var e = e || window.event
                var target = e.target || target.srcElement
                if (target.className == 'caidan') {
                    target.children[0].children[0].style.display = 'none'
                    target.children[0].children[1].style.display = 'block'
                    target.children[1].style.display = 'block'
                }
            })
            this.nav.addEventListener('mouseout', (e) => {
                var e = e || window.event
                var target = e.target || target.srcElement
                if (target.className == 'catalog') {
                    target.parentNode.children[0].children[0].style.display = 'block'
                    target.parentNode.children[0].children[1].style.display = 'none'
                    target.style.display = 'none'
                }
            })
        }
    }
    new Xiala()

    //购物车数量
    let list = JSON.parse(localStorage.getItem('cart')) || []
    let count = 0
    list.map((item) => {
        count += item.cart_number
    })
    em.innerHTML = count

});






// 商品渲染+加购
// class List {
//     constructor() {
//         this.ul = document.querySelector('.list')
//         this.em = document.querySelectorAll('em')[0]
//         this.btn = document.querySelector('button')
//         this.init()
//     }
//     init() {
//         this.request()
//         this.add()
//此处再次调用，刷新不会清零
//     this.count()
// }
// request() {
//     pAjax({
//             url: './data/goods.json'
//         })
//         .then(data => {
//             this.datalist = JSON.parse(data).slice(0, 20)
//             this.render(this.datalist)
//         })
// }
// render(data) {
//     console.log(data);
//     this.datalist.forEach(item => {
//         this.ul.innerHTML += `
// <li>
//     <a href=""><img src="${item.img_small_logo}">
//     <p class="name ellipse">${item.title}</p>
//     <p class="price">¥${item.price}</p></a>
//     <button data-id=${item.goods_id}>加入购物车</button>
// </li>
// `
//     })
// }
// add() {
//     this.ul.onclick = (e) => {
//         var e = e || window.event
//         var target = e.target || e.srcElement
//         console.log(target);
//         if (target.nodeName == 'BUTTON') {
//             let targetId = target.dataset.id
//找到当前数据
// let goods = this.datalist.find((item) => {
//     return item.goods_id == targetId
// })
// console.log(goods);
// let list = JSON.parse(localStorage.getItem('cart')) || []
// if (list.length > 0) {
//     let result = list.some((item) => {
//         return item.goods_id == targetId
//     })
//     if (result) {
//         let index = list.findIndex((item) => {
//             return item.goods_id == targetId
//         })
//         list[index].cart_number++
//         if (goods.cart_number > goods.goods_number) {
//             alert('库存不足')
//         }
//     } else {
//         goods.cart_number = 1
//         list.push(goods)
//     }
// } else {
//     goods.cart_number = 1
//     list.push(goods)
// }
// localStorage.setItem('cart', JSON.stringify(list))
//在此调用，点击button才会显示在购物车处
//                 this.count()
//             }
//         }
//     }
//     count() {
//         let list = JSON.parse(localStorage.getItem('cart')) || []
//         let count = 0
//         list.map((item) => {
//             count += item.cart_number
//         })
//         this.em.innerHTML = count
//     }
// }
// new List()